﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLibrary.BLL;
using System.Data.SqlClient;

namespace BusinessLibrary.DAL
{
    class sqlClasse
    {
        private static Classe ReaderToObject(SqlDataReader dr)
        {
            Classe _classe = new Classe();
            _classe.Id = int.Parse(dr["Id"].ToString());

            _classe.Nom = dr["Nom"].ToString();



            if (!object.ReferenceEquals(dr["Id_niveau"], DBNull.Value))
            {
                _classe.niveau = Niveau.GetNiveauById(int.Parse(dr["Id_niveau"].ToString()));
            }
            return _classe;
        }

        public static Classe GetClasseById(int id)
        {
            Classe classe = new Classe();
            string strSQL = @"select * from Classe where UserId=@Id";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);
                    cmd.Parameters.AddWithValue("@Id", id);
                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        classe = ReaderToObject(dr);
                    }
                    dr.Close();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return classe;
        }

        public static int insertClasse(Classe c)
        {
            int n = 0;
            string strSQL = @"insert into Classe (Nom,Id_niveau) values(@nom,@id_niveau)";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);

                    cmd.Parameters.AddWithValue("@nom", c.Nom);
                    cmd.Parameters.AddWithValue("@id_niveau", c.niveau.Id);

                    n = cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int updateClasse(Classe c)
        {
            int n = 0;

            string strSQl = @"UPDATE Classe SET (@nom IS NULL OR Nom=@nom), Id_niveau=@id_niveau where Id=@id ";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();

                    SqlCommand cmd = new SqlCommand(strSQl, cnx);

                    cmd.Parameters.AddWithValue("@nom", c.Nom);
                    cmd.Parameters.AddWithValue("@id_niveau", c.niveau.Id);
                    n = cmd.ExecuteNonQuery();

                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="idAuthentif"></param>
        /// <returns></returns>
        public static int deletClasse(int id, int idniveau)
        {
            int n = 0;
            string strSQL = @"delete from Classe where id=@id)";

            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);

                    cmd.Parameters.AddWithValue("@id", id);


                    n = cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }
        public static List<Classe> SelectListeClasse()
        {
            string strSQL = @"select * from Classe ";
            using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
            {
                cnx.Open();

                SqlCommand cmd = new SqlCommand(strSQL, cnx);


                SqlDataReader dr = cmd.ExecuteReader();

                List<Classe> List_classe = new List<Classe>();
                while (dr.Read())
                {
                    List_classe.Add(ReaderToObject(dr));
                }

                dr.Close();
                cnx.Close();

                return List_classe;
            }
        }
    }
}
